<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Controller;

use Xcx\Model\XcxModel;
use Xcx\Model\UserModel;

class LoginController extends XcxBaseController {

    public function __construct() {
        parent::__construct();
    }

    public function login() {
        $xcxMod = new XcxModel();
        /**
         * 3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
         */
        $code = I("post.code", '', 'htmlspecialchars_decode');
        $rawData = I("post.rawData", '', 'htmlspecialchars_decode');
        $signature = I("post.signature", '', 'htmlspecialchars_decode');
        $encryptedData = I("post.encryptedData", '', 'htmlspecialchars_decode');
        $iv = I("post.iv", '', 'htmlspecialchars_decode');

        $res = $xcxMod->login($code, $rawData, $signature, $encryptedData, $iv);
        if ($res['status'] == 1) {

            $map['openid'] = $res['result']['openId'] ? $res['result']['openId'] : '';
            $map['unionid'] = $res['result']['unionId']? $res['result']['unionId'] : '';
            $map['oauth'] = 'weixin';
            $map['sex'] = $res['result']['gender']? $res['result']['gender'] : '1';
            $map['nickname'] = empty($res['result']['nickName']) ? 'xiarikui_' . rand(1, 9999) : $res['result']['nickName'];
            $map['head_pic'] = empty($res['result']['avatarUrl']) ? '' : $res['result']['avatarUrl'];
            //$map['is_app'] = 1;
            $userMod = new UserModel();
            $result = $userMod->thirdLogin($map);
            if($result['status'] == 1) {
                $result['result']['head_pic'] = $map['head_pic'];
                jsonReturn($result['result']);
            }else{
                jsonReturn(0,$result['msg']);
            }
        }

        jsonReturn(0,$res['msg']);
    }


}
